import java.io.File;

import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;
import lejos.nxt.Sound;
import lejos.nxt.addon.IRSeekerV2;
import lejos.nxt.addon.IRSeekerV2.Mode;
import lejos.util.Delay;


public class PreyMain {
	private static IRSeekerV2 seeker;
	public static int angle60 = -60;
	public static int angle30 = -30;
	public static int max60 = 60;
	public static int max30 = 70;
	public static int delays = 300;
	

	public static void main(String[] args) {
		LCD.drawString("Press button", 0, 0);
    	Button.waitForAnyPress();
    	LCD.clear();
    	

    	
    	//Sound.setVolume(Sound.VOL_MAX);
    	//Sound.playSample(new File("LionRoar2.wav"),100);
    	seeker = new IRSeekerV2(SensorPort.S2, Mode.DC);
    	
    	int direction;
    	int sensorVal[] = new int[5];
    	while ( ! Button.ESCAPE.isDown())
        {	
    		for(int i = 1; i <= 5; i++)
    		{
    			sensorVal[i-1] = seeker.getSensorValue(i);
    			LCD.drawInt(sensorVal[i-1], 3, 0, i);
    		}
    		
    		int max = 0;
    		for(int i = 0; i < 5; i++)
    		{
    			if(sensorVal[i] > max)
    				max = sensorVal[i];
    		}
    		
    		direction = seeker.getDirection();
    		LCD.drawInt(direction, 3, 0, 7);
    		
    		float angle = seeker.getAngle();
    		String t = String.valueOf(angle);
    		LCD.drawString(t, 5, 7);
    		
    		LCD.drawString("max = ", 5, 0);
    		LCD.drawInt(max, 11, 0);
    		if(max < 100)
    			LCD.drawString("  ", 13, 0);
    		
    		if(max > 30)
    		{
	    		if(direction < 5 & direction > 0)
	    		{
	    			LCD.drawString("Dir less rotate", 5, 1);
					Car.drive(80, 0);
					Delay.msDelay(delays);
					Car.stop();
	    		}
	    		else if(direction > 5)
	    		{
	    			LCD.drawString("Dir more rotate", 5, 1);
	    			Car.drive(0, 80);
					Delay.msDelay(delays);
					Car.stop();
	    		}
	    		else if(direction == 5)
	    		{
	    			LCD.drawString("Dir drive      ", 5, 1);
					Car.drive(70, 70);
					Delay.msDelay(delays*2);
					Car.stop();
	    		}
    		}
    		else
    		{
    			Car.stop();
    		}
    		

			/*switch(direction)
			{
			case 1:
			case 2:
				if(-120 < angle & angle < -45 & max > max60)
				{
					LCD.drawString("Dir 1 rotate", 5, 1);
					Motor.A.rotate(angle60);
				}
				break;
			case 3: 
				if(max > max60)
				{
					if(-45 < angle & angle < 0)
					{
						LCD.drawString("Dir 2 rotate", 5, 1);
						Motor.A.rotate(angle60);
					}
					else if(-120 < angle & angle < -45)
					{
						LCD.drawString("Dir 2 rotate", 5, 1);
						Motor.A.rotate(angle30);
					}
				}
				break;
			case 4:
				if(max > max30)
				{
					if(-20 < angle & angle < 20)
					{
						LCD.drawString("Dir 4 drive", 5, 1);
						car.drive(70, 70);
						Delay.msDelay(500);
						car.stop();
					}
					else if(-45 < angle & angle < -20)
					{
						LCD.drawString("Dir 4 rotate", 5, 1);
						Motor.A.rotate(angle30);
					}
				}
				break;
			case 5:
				if(max > max30)
				{
					if(-20 < angle & angle < 20)
					{
						LCD.drawString("Dir 5 drive ", 5, 1);
						car.drive(70, 70);
						Delay.msDelay(500);
						car.stop();
					}
					else if(-45 < angle & angle < -20)
					{
						LCD.drawString("Dir 5 rotate", 5, 1);
						Motor.A.rotate(angle60);
					}
					else if(20 < angle & angle < 45)
					{
						LCD.drawString("Dir 5 rotate", 5, 1);
						Motor.C.rotate(angle30);
					}
				}
				break;
			case 6:
				if(max > max30)
				{
					if(-20 < angle & angle < 20)
					{
						LCD.drawString("Dir 6 drive ", 5, 1);
						car.drive(70, 70);
						Delay.msDelay(500);
						car.stop();
					}
					else if(20 < angle & angle < 45)
					{
						LCD.drawString("Dir 6 rotate", 5, 1);
						Motor.C.rotate(angle30);
					}
				}
				break;
			case 7:
				if(max > max30)
				{
					if(20 < angle & angle < 45)
					{
						LCD.drawString("Dir 7 rotate", 5, 1);
						Motor.C.rotate(angle30);
					}
					else if(45 < angle & angle < 120)
					{
						LCD.drawString("Dir 7 rotate", 5, 1);
						Motor.C.rotate(angle60);
					}
				}
				break;
			case 8:
			case 9:
				if(45 < angle & angle < 120 & max > max60)
				{
					LCD.drawString("Dir 8 rotate", 5, 1);
					Motor.C.rotate(angle60);
				}
				break;
			default:
				LCD.drawString("No target", 5, 1);	
			}*/
    			
    		
    					
    		//direction = seeker.getDirection();
    		//float angle = seeker.getAngle();
    		
    		
    		Delay.msDelay(500);
    		
    		
   
    		
    		
    		
    		
    		//Sound.twoBeeps();
    		//Delay.msDelay(200);
    		//Sound.playSample(new File("Test2.wav"),100);
            //LCD.drawString("Playing", 0, 3);
        }

	}

}
